<html>
<head><meta charset="utf-8"><title>Questions regarding Heap allocation in const eval · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html">Questions regarding Heap allocation in const eval</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216632678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216632678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216632678">(Nov 13 2020 at 16:08)</a>:</h4>
<p>I'm trying to read up on heap allocation and in const eval, and I have a lot of questions <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> </p>
<p>the first comment on the issue says</p>
<div class="codehilite"><pre><span></span><code>const B: String = String::from(&quot;foo&quot;); // Not OK
</code></pre></div>
<p>Is this going to be never supported?</p>



<a name="216634146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216634146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216634146">(Nov 13 2020 at 16:17)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"foo"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="n">FOO</span>: <span class="nc">Foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Foo</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FOO</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"foo"</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>


<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">foo</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FOO</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"hi"</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Isn't <code>drop</code> similar to a destructor? Having a non copyable value dropping twice feels weird to me.</p>



<a name="216634975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216634975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216634975">(Nov 13 2020 at 16:23)</a>:</h4>
<p>well... if you can create multiple somethings by using a constant twice, you could also create the same thing by repeating the initializer expression of the constant directly in your code.</p>



<a name="216635048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216635048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216635048">(Nov 13 2020 at 16:24)</a>:</h4>
<p>I guess this is where private fields become interesting, since you could have a public constant but no other way to construct values of something</p>



<a name="216635121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216635121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216635121">(Nov 13 2020 at 16:24)</a>:</h4>
<p>That said, your code already works on stable <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="216635183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216635183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216635183">(Nov 13 2020 at 16:25)</a>:</h4>
<p>the same with <code>const FOO: String = String::new();</code></p>



<a name="216724129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216724129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216724129">(Nov 14 2020 at 11:42)</a>:</h4>
<p>when i do</p>
<div class="codehilite"><pre><span></span><code>let f = FOO;
</code></pre></div>
<p>isn't it more or less doing an implicit copy of the struct?</p>



<a name="216726310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216726310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216726310">(Nov 14 2020 at 12:28)</a>:</h4>
<p>yes, BUT... <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span></p>



<a name="216726322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216726322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216726322">(Nov 14 2020 at 12:29)</a>:</h4>
<p>if you can write <code>let f = String::new();</code> and <code>const FOO: String = String::new();</code> then you can write <code>let f = FOO;</code> as you could just have taken the body of <code>FOO</code> and written <code>let f = String::new();</code></p>



<a name="216726330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216726330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216726330">(Nov 14 2020 at 12:29)</a>:</h4>
<p>basically we have the invariant that all constants are <em>values</em> that can be bitwise copied, even if their <em>types</em> forbid copying values</p>



<a name="216726386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216726386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216726386">(Nov 14 2020 at 12:30)</a>:</h4>
<p>this is why the const heap situation is so complicated. <code>const FOO: String = "foo".to_string();</code> can never be legal.</p>



<a name="216726390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216726390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216726390">(Nov 14 2020 at 12:30)</a>:</h4>
<p>because that is a value that cannot be bitwise copied, it must be cloned.</p>



<a name="216726396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216726396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216726396">(Nov 14 2020 at 12:31)</a>:</h4>
<p>(copying here means bitwise copies, editing my messages to reflect that)</p>



<a name="216727248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216727248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216727248">(Nov 14 2020 at 12:44)</a>:</h4>
<p>okay, i think i'm a bit confused here.</p>
<p>in DMs you had said <code>const fn muh() -&gt; Vec&lt;i32&gt; { vec![42] }</code> is something we'd want to support in the future,</p>
<p>So, if i'm reading it correctly, we'd support <code>const fn muh() -&gt; Vec&lt;i32&gt; { vec![42] }</code> but not <code>const V: Vec&lt;i32&gt; = muh()</code>. is this right?</p>



<a name="216728136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728136">(Nov 14 2020 at 13:01)</a>:</h4>
<p>yes, but now comes the freaky part, we already support <code>const fn blub() -&gt; Vec&lt;i32&gt; { Vec::new() } const W: Vec&lt;i32&gt; = blub();</code> and we want to keep supporting it.</p>
<p>If memory serves (and a quick skim of the const-eval#20 confirms), we'll want to forbid doing actual heap allocations via the generic <a href="https://doc.rust-lang.org/std/alloc/struct.Global.html"><code>Global</code> heap</a>, and require a specific <code>ConstHeap</code> to be used. Once <code>Vec</code> gets a second generic parameter for its allocator (just like <code>Box</code> has), <code>Vec::new()</code> will still be in the <code>impl&lt;T, A&gt; Vec&lt;T, A&gt;</code> block, where <em>no</em> bounds are on <code>A</code>, so we know at compile-time that this function can be called just fine without specifying an allocator, but any use of an <a href="https://doc.rust-lang.org/std/alloc/trait.AllocRef.html"><code>AllocRef</code></a> that is not <code>ConstAlloc</code> will not work <em>anyway</em> because we just won't mark them <code>const impl</code></p>



<a name="216728187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728187">(Nov 14 2020 at 13:02)</a>:</h4>
<p>This is a bad brain dump, I apologize. Let's write up a hackmd? I think that serves the design purpose better than a chat</p>



<a name="216728483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728483">(Nov 14 2020 at 13:11)</a>:</h4>
<p>yeah, that's better. i'll start doing that,</p>



<a name="216728541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728541">(Nov 14 2020 at 13:13)</a>:</h4>
<p>Here's the empty (for now) hackmd: <a href="https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA">https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA</a></p>



<a name="216728941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728941">(Nov 14 2020 at 13:25)</a>:</h4>
<p>I don't remember that "alternative allocator" approach but it is intriguing</p>



<a name="216728985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728985">(Nov 14 2020 at 13:26)</a>:</h4>
<p>I wonder however if it is not too restrictive -- making existing code const-callable would now require also making it generic over the allocator</p>



<a name="216728987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728987">(Nov 14 2020 at 13:26)</a>:</h4>
<p>if that is an okay restriction, it seems however quite elegant indeed :D</p>



<a name="216728999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216728999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216728999">(Nov 14 2020 at 13:27)</a>:</h4>
<p>the issue still links to <a href="https://github.com/rust-lang/const-eval/issues/20#issuecomment-468657992">this</a> as the current state, is that accurate?</p>



<a name="216729114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216729114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216729114">(Nov 14 2020 at 13:30)</a>:</h4>
<blockquote>
<p>I've been reading const-eval#20, if I understand it correctly, we haven't reached a consensus on the design yet? Or is this more or less the consensus ?</p>
<p>oli<br>
well.. yes and no. The "yes" part is that we need that anyway for anything that will get stabilized. The reason for that is that we have a static/dynamic check system which guarantees that the dynamic checks are the ones making everything sound and the static checks approximate the dynamic checks (so they reject more code than the dynamic checks) in order to make everything sane (read: not footgunny and easier to understand)<br>
so we do need the dynamic checks on the final value, but for stabilization we'll also need to come up with a static check system</p>
</blockquote>



<a name="216729162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216729162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216729162">(Nov 14 2020 at 13:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216728985">said</a>:</p>
<blockquote>
<p>I wonder however if it is not too restrictive -- making existing code const-callable would now require also making it generic over the allocator</p>
</blockquote>
<p>We do not need to change existing const-callable code, as long as you don't actually allocate, you won't be calling code which contains any allocators. Only when you extend the allocating parts of your code will you need to make it generic</p>



<a name="216729220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216729220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216729220">(Nov 14 2020 at 13:32)</a>:</h4>
<p>this is all very confusing, thus my ramblings above. We'll write down all the details and then present it.</p>



<a name="216729691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216729691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216729691">(Nov 14 2020 at 13:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216729220">said</a>:</p>
<blockquote>
<p>this is all very confusing, thus my ramblings above. We'll write down all the details and then present it.</p>
</blockquote>
<p>using the type system to track what kind of allocation you have is the least confusing proposal in this space that I have seen so far^^</p>



<a name="216729779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216729779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216729779">(Nov 14 2020 at 13:42)</a>:</h4>
<blockquote>
<p>We do not need to change existing const-callable code, as long as you don't actually allocate, you won't be calling code which contains any allocators. Only when you extend the allocating parts of your code will you need to make it generic</p>
</blockquote>
<p>What I mean is, when I want to, dunno, use serde_json during CTFE, then not only will serde_json have to add <code>const</code> everywhere, it'll also have to be made generic over the allocator</p>



<a name="216730423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216730423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216730423">(Nov 14 2020 at 13:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216729779">said</a>:</p>
<blockquote>
<p>What I mean is, when I want to, dunno, use serde_json during CTFE, then not only will serde_json have to add <code>const</code> everywhere, it'll also have to be made generic over the allocator</p>
</blockquote>
<p>Maybe we can come up with some advanced hackery for transient allocations?</p>



<a name="216730646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216730646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216730646">(Nov 14 2020 at 13:58)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216730423">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216729779">said</a>:</p>
<blockquote>
<p>What I mean is, when I want to, dunno, use serde_json during CTFE, then not only will serde_json have to add <code>const</code> everywhere, it'll also have to be made generic over the allocator</p>
</blockquote>
<p>Maybe we can come up with some advanced hackery for transient allocations?</p>
</blockquote>
<p>that's actually not hard <em>in principle</em> -- interning could reject <code>Global</code> allocations and accept only <code>ConstHeap</code> allocations</p>



<a name="216730655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216730655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216730655">(Nov 14 2020 at 13:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216730646">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216730423">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/216729779">said</a>:</p>
<blockquote>
<p>What I mean is, when I want to, dunno, use serde_json during CTFE, then not only will serde_json have to add <code>const</code> everywhere, it'll also have to be made generic over the allocator</p>
</blockquote>
<p>Maybe we can come up with some advanced hackery for transient allocations?</p>
</blockquote>
<p>that's actually not hard <em>in principle</em> -- interning could reject <code>Global</code> allocations and accept only <code>ConstHeap</code> allocations</p>
</blockquote>
<p>I was hoping for a static check</p>



<a name="216730876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216730876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216730876">(Nov 14 2020 at 14:05)</a>:</h4>
<p>that seems like a problem very similar to <code>&amp;mut</code> -- allwoing things internally but not allowing them to "leak" to the final value</p>



<a name="216731417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216731417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216731417">(Nov 14 2020 at 14:19)</a>:</h4>
<p>yea, I was thinking about a dataflow analysis, too, but in contrast to <code>&amp;mut</code> we don't have the borrowchecker that handles this for us</p>



<a name="216731473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216731473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216731473">(Nov 14 2020 at 14:20)</a>:</h4>
<p>like <code>const FOO: &amp;mut i32 = { let mut x = 42; &amp;mut x };</code> doesn't work because of the borrowchecker, not because of the dataflow analysis that prevents <code>const FOO: &amp;mut i32 = &amp;mut 42;</code></p>



<a name="216731545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216731545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216731545">(Nov 14 2020 at 14:22)</a>:</h4>
<p>with heap stuff we have owned things, so we'd need to prevent <code>const FOO: String = { let s = "foo".to_string(); s };</code>. If transient heap allocations use <code>Global</code>, what prevents us from putting them into the final value?</p>



<a name="216732165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216732165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216732165">(Nov 14 2020 at 14:36)</a>:</h4>
<p>the borrow checker will sop helping even for <code>&amp;mut</code> the moment we make <code>Box::leak</code> a <code>const fn</code></p>



<a name="216732407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216732407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216732407">(Nov 14 2020 at 14:41)</a>:</h4>
<p>I don't think that <code>Box::leak</code> is relevant here, it has the appropriate lifetimes. If <code>T: 'a</code>and <code>'a != 'static</code> you'll get a <code>&amp;'a T</code> and never a <code>&amp;'static T</code></p>



<a name="216732437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216732437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216732437">(Nov 14 2020 at 14:41)</a>:</h4>
<p>So if you do <code>Box::leak(Box::new(&amp;mut x))</code> for <code>let mut x = 42;</code>, then you can't move that into the return value except by going through raw pointers, which will just cause a dangling pointer error</p>



<a name="216733390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216733390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216733390">(Nov 14 2020 at 14:57)</a>:</h4>
<p>what I mean is, you end up with <code>const FOO: &amp;mut i32 = { let mut x = 42; Box::leak(Box::new(x)) };</code></p>



<a name="216733392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216733392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216733392">(Nov 14 2020 at 14:57)</a>:</h4>
<p>so we can now have <code>&amp;mut</code> leak into the final value</p>



<a name="216733437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216733437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216733437">(Nov 14 2020 at 14:58)</a>:</h4>
<p>oh heh</p>



<a name="216825547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216825547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216825547">(Nov 16 2020 at 01:42)</a>:</h4>
<p>What about requiring the final <code>const</code> to be <code>Copy</code> if it contains any pointers to allocated data?</p>



<a name="216825578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216825578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216825578">(Nov 16 2020 at 01:43)</a>:</h4>
<p>non-<code>Copy</code> types work if you have the final <code>const</code>be a shared reference to your type.</p>



<a name="216852566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216852566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216852566">(Nov 16 2020 at 10:12)</a>:</h4>
<p>The problem is that any scheme which analyzes the final value for heap pointers will not work for associated consts of generic traits</p>



<a name="216852583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216852583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216852583">(Nov 16 2020 at 10:12)</a>:</h4>
<p>because you don't have a final value yet</p>



<a name="216852621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/216852621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#216852621">(Nov 16 2020 at 10:13)</a>:</h4>
<p>So you may have broken code that only errors when you use it in the end. This can cause situations similar to duck typing generics</p>



<a name="217146146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217146146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217146146">(Nov 18 2020 at 15:05)</a>:</h4>
<p>So... I've been typing up <a href="https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA">https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA</a> and realized that trying some sort of <code>ConstAlloc</code> scheme can't really work, because we won't be able to track that through <code>const fn foo() -&gt; SomeStruct { SomeStruct { v: vec![42] } }</code> as no part of the function call requires any alloc scheme. Even if we made <code>Global</code>'s <code>Alloc</code> impl not <code>const</code>, we could have <code>foo</code> but the type of <code>v</code> is <code>Vec&lt;i32, ConstAlloc&gt;</code>, and we'd again not see this via any kind of static analysis</p>



<a name="217146275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217146275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217146275">(Nov 18 2020 at 15:06)</a>:</h4>
<p>we could add a "look at bodies" scheme for finding heap operations in <code>const fn</code>, but I'd really like to come up with something that does not require that</p>



<a name="217181603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217181603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217181603">(Nov 18 2020 at 19:05)</a>:</h4>
<p>Is the following possible?</p>
<p>Can we only do <code>ConstSafe</code> for Vec with ConstAlloc</p>
<div class="codehilite"><pre><span></span><code>unsafe impl&lt;A: ConstAllocTrait, T&gt; ConstSafe for Vec&lt;T, A&gt;
</code></pre></div>
<p>?</p>



<a name="217182107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217182107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217182107">(Nov 18 2020 at 19:09)</a>:</h4>
<p>On second thought, even if it's possible, it'll break <code>Vec::new()</code>, I guess</p>



<a name="217183981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217183981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217183981">(Nov 18 2020 at 19:23)</a>:</h4>
<p>what about adding a new <code>const(alloc)</code> function type (there's probably a better name) which would allow associated/plain constants to enforce that their initializers must either not contain any <code>const(alloc)</code> function calls or their types must be copy (or at least not require any <code>Drop</code> code to be run). That way:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">S</span>: <span class="nb">String</span> <span class="o">=</span><span class="w"> </span><span class="nb">String</span>::<span class="n">new</span><span class="p">();</span><span class="w"> </span><span class="c1">// works since `new` is not const(alloc)</span>
<span class="k">const</span><span class="w"> </span><span class="n">S2</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">static</span> <span class="nb">String</span> <span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">"blah {}"</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"> </span><span class="c1">// works since final type is `Copy`</span>
<span class="k">const</span><span class="w"> </span><span class="n">S3</span>: <span class="nb">String</span> <span class="o">=</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"a"</span><span class="p">);</span><span class="w"> </span><span class="c1">// error because you called a const(alloc) fn and final type is not Copy</span>
</code></pre></div>



<a name="217185318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217185318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217185318">(Nov 18 2020 at 19:33)</a>:</h4>
<p>I think Copy would be required otherwise you could have:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">S</span>: <span class="nc">ManuallyDrop</span><span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ManuallyDrop</span>::<span class="n">new</span><span class="p">(</span><span class="s">"a"</span><span class="p">.</span><span class="n">into</span><span class="p">());</span><span class="w"></span>
<span class="n">S</span><span class="p">.</span><span class="n">into_inner</span><span class="p">();</span><span class="w"> </span><span class="c1">// UB</span>
</code></pre></div>



<a name="217187021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217187021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217187021">(Nov 18 2020 at 19:46)</a>:</h4>
<p>Isn't there still a possibility that a const(alloc) function would be using an associated trait?</p>



<a name="217187063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217187063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217187063">(Nov 18 2020 at 19:46)</a>:</h4>
<blockquote>
<p>So you may have broken code that only errors when you use it in the end. This can cause situations similar to duck typing generics</p>
</blockquote>
<p>This won't be avoidable in that case</p>



<a name="217240133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217240133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217240133">(Nov 19 2020 at 08:26)</a>:</h4>
<p>yea... an effect sytem (so something similar to the <code>const(alloc)</code> thing) is a big big hammer with lots of nuance that we may want to avoid</p>



<a name="217240438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217240438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217240438">(Nov 19 2020 at 08:30)</a>:</h4>
<p>We can essentially achieve <code>const(alloc)</code> by forbidding the use of the <code>Const</code> allocator in <code>const fn</code>, thus all <code>const fn</code> must either not allocate or have a generic parameter that const and static items (which are allowed to use it), can then pass in</p>



<a name="217240468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217240468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217240468">(Nov 19 2020 at 08:31)</a>:</h4>
<p>though this would completely forbid the use of <code>serde_json</code> without making it very generic</p>



<a name="217370254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217370254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217370254">(Nov 20 2020 at 07:47)</a>:</h4>
<p>I think <code>const(alloc)</code> (or something with similar effect) and specifically using the default Rust allocator for const allocation would be more ergonomic to use, since we don't have to use different allocator types everywhere just to enable const alloc. We could potentially have a <code>ConstAllocSafe</code> auto trait that types implement to indicate that they don't deallocate pointers that could come from constants. So, <code>Box&lt;String&gt;</code> would implement <code>ConstAllocSafe</code> because using a <code>const</code> <code>&amp;Box&lt;String&gt;</code> can't cause deallocation of the <code>const</code>'s pointers. Nearly all types would be <code>ConstAllocSafe</code>. (I can't think of any counter-examples off the top of my head...)</p>



<a name="217380623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217380623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217380623">(Nov 20 2020 at 09:57)</a>:</h4>
<p>My problem with a <code>const(alloc)</code> approach is that I think this would mean we would also have to have this on function pointers, dynamic trait objects, generic bounds</p>



<a name="217499992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217499992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217499992">(Nov 21 2020 at 12:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/217146146">said</a>:</p>
<blockquote>
<p>So... I've been typing up <a href="https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA">https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA</a> and realized that trying some sort of <code>ConstAlloc</code> scheme can't really work, because we won't be able to track that through <code>const fn foo() -&gt; SomeStruct { SomeStruct { v: vec![42] } }</code> as no part of the function call requires any alloc scheme. Even if we made <code>Global</code>'s <code>Alloc</code> impl not <code>const</code>, we could have <code>foo</code> but the type of <code>v</code> is <code>Vec&lt;i32, ConstAlloc&gt;</code>, and we'd again not see this via any kind of static analysis</p>
</blockquote>
<p>basically, to support this the code would need to change to <code>const fn foo() -&gt; SomeStruct&lt;Alloc&gt;</code></p>



<a name="217500049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217500049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217500049">(Nov 21 2020 at 12:35)</a>:</h4>
<p>everything needs to be generic</p>



<a name="217500056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217500056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217500056">(Nov 21 2020 at 12:35)</a>:</h4>
<p>the weirdest part though is that the type of an allocation depends on whether it happens at runtime or compiletime... no idea how to do that...^^</p>



<a name="217547951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217547951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217547951">(Nov 22 2020 at 11:56)</a>:</h4>
<p>my problem is that <code>ConstAlloc</code> would be "globally available", so you can just create a <code>struct Foo(*mut i32);</code> and from that noone can tell whether an alloc happened or not. So if we have a <code>const fn foo() -&gt; Foo</code>, how do we analyze the body of that function to ensure it does not put a heap pointer into the result?</p>



<a name="217547971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217547971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217547971">(Nov 22 2020 at 11:57)</a>:</h4>
<p>One way would be to make <code>ConstAlloc</code> only be available in <code>const</code> and <code>static</code> items and not in <code>const fn</code>. Thus it would need to get passed via generics, otherwise you don't have any way to create heap allocs</p>



<a name="217548019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548019">(Nov 22 2020 at 11:58)</a>:</h4>
<p>this would not require any fancy scheme and would just work <span aria-label="tm" class="emoji emoji-2122" role="img" title="tm">:tm:</span></p>



<a name="217548025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548025">(Nov 22 2020 at 11:58)</a>:</h4>
<p>but it would require everything to become generic over the alloc type</p>



<a name="217548043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548043">(Nov 22 2020 at 11:59)</a>:</h4>
<p>Since things like <code>serde_json</code> would need to put <code>const</code> everywhere in order to be come usable in const, it doesn't seem too bad to also require being generic over the <code>Alloc</code> type as long as we have defaults for parameters, too</p>



<a name="217548366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548366">(Nov 22 2020 at 12:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/217547951">said</a>:</p>
<blockquote>
<p>my problem is that <code>ConstAlloc</code> would be "globally available", so you can just create a <code>struct Foo(*mut i32);</code> and from that noone can tell whether an alloc happened or not. So if we have a <code>const fn foo() -&gt; Foo</code>, how do we analyze the body of that function to ensure it does not put a heap pointer into the result?</p>
</blockquote>
<p>I thought with <code>ConstAlloc</code> we don't need to tell that any more, is the entire point</p>



<a name="217548455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548455">(Nov 22 2020 at 12:11)</a>:</h4>
<p>oh... because it doesn't do anything on drop <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="217548457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548457">(Nov 22 2020 at 12:11)</a>:</h4>
<p>I completely confused myself</p>



<a name="217548510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548510">(Nov 22 2020 at 12:13)</a>:</h4>
<p>so yea, we can't do transient allocations via <code>Global</code> at all, because we can't really enforce that you don't transmute things so badly that we lose track of them, but if we just never make the <code>Alloc</code> impl for <code>Global</code> <code>const</code>, and only do <code>impl const Alloc for ConstAlloc</code>, then we're absolutely fine</p>



<a name="217548519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217548519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217548519">(Nov 22 2020 at 12:13)</a>:</h4>
<blockquote>
<p>the weirdest part though is that the type of an allocation depends on whether it happens at runtime or compiletime... no idea how to do that...^^</p>
</blockquote>
<p>can you elaborate on that? I'm not sure what you mean</p>



<a name="217556846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217556846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217556846">(Nov 22 2020 at 15:44)</a>:</h4>
<blockquote>
<p>so yea, we can't do transient allocations via Global at all, because we can't really enforce that you don't transmute things so badly that we lose track of them,</p>
</blockquote>
<p>We can, but only dynamically</p>



<a name="217557116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217557116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217557116">(Nov 22 2020 at 15:51)</a>:</h4>
<p>yea, that's true</p>



<a name="217557122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217557122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217557122">(Nov 22 2020 at 15:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/217548519">said</a>:</p>
<blockquote>
<blockquote>
<p>the weirdest part though is that the type of an allocation depends on whether it happens at runtime or compiletime... no idea how to do that...^^</p>
</blockquote>
<p>can you elaborate on that? I'm not sure what you mean</p>
</blockquote>
<p>if we want to have a function that e.g. creates a Box and works both at compiletime and runtime... in one case it returns <code>Box&lt;T, Global&gt;</code>, in the other <code>Box&lt;T, ConstGlobal&gt;</code></p>



<a name="217557389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217557389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217557389">(Nov 22 2020 at 15:58)</a>:</h4>
<p>well... you'd have to specify this <code>Alloc</code> type somewhere, we won't have magical insertion of <code>ConstGlobal</code> just because you are in a const context. At least I have no idea how we could do that. So yea, as I see it, we'll have different types, but invoking <code>Box::new(42)</code> will <em>always</em> return a <code>Box&lt;i32, Global&gt;</code>, unless inference figures out that we need a <code>ConstGlobal</code></p>



<a name="217558882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217558882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217558882">(Nov 22 2020 at 16:37)</a>:</h4>
<p>yeah. this means code that returns allocated objects needs to not just be made <code>const fn</code> but be adjusted to be able to work with different allocators. not sure how people would react to that.</p>



<a name="217559226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217559226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217559226">(Nov 22 2020 at 16:44)</a>:</h4>
<p>This may also be messy around trait methods returning <code>Box</code>es and similar, as we want to keep them object safe if they were before. But I guess these problems <em>also</em> come up if you just consider custom allocators in isolation</p>



<a name="217559318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217559318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217559318">(Nov 22 2020 at 16:46)</a>:</h4>
<p>I was thinking for a moment that we could also just use <code>Global</code> and do some magic at the <code>const</code>/<code>static</code> item boundary that changes the type to <code>ConstGlobal</code>, but that won't work if you have a <code>struct Foo(*mut i32);</code> that has methods to take out the inner thing's ownership and yield a <code>Box&lt;i32, Global&gt;</code></p>



<a name="217559351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217559351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217559351">(Nov 22 2020 at 16:47)</a>:</h4>
<p>I mean... that could all work if we want transient allocations anyway and just permit types with <code>unsafe impl Const(Ref)Safe</code> impls</p>



<a name="217559419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217559419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217559419">(Nov 22 2020 at 16:48)</a>:</h4>
<p>if we take the post-monomorphization error on leaked heap allocs <em>anyway</em>, we can also go full in and only implement <code>ConstRefSafe</code> for <code>Box&lt;T,  Global&gt;</code> but not auto-implement for any types beyond the primitives</p>



<a name="217559440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/217559440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#217559440">(Nov 22 2020 at 16:49)</a>:</h4>
<p>Then <code>Foo</code> would not be valid in the final value of a constant</p>



<a name="218075911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218075911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218075911">(Nov 27 2020 at 10:22)</a>:</h4>
<p>ok... So... I found a problem with our current plan. It would be a breaking change. When looking at <a href="https://github.com/rust-lang/rust/pull/78461">https://github.com/rust-lang/rust/pull/78461</a> by <span class="user-mention" data-user-id="216785">@Tim Diekmann</span> I realized that <a href="https://github.com/rust-lang/wg-allocators/issues/1#issuecomment-488990394">it's a breaking change</a> to make <code>Vec::new</code> generic over the <code>AllocRef</code> being used. I'm not sure in what situations it's a problem.</p>



<a name="218076019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076019">(Nov 27 2020 at 10:24)</a>:</h4>
<p>Maybe I confused my self thoroughly with all the back and forth. So let's start from the beginning</p>



<a name="218076071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076071">(Nov 27 2020 at 10:24)</a>:</h4>
<p>We want</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">FOO</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
</code></pre></div>
<p>to keep working</p>



<a name="218076135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076135">(Nov 27 2020 at 10:25)</a>:</h4>
<p>That desugars to</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">FOO</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">Global</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">Global</span><span class="o">&gt;</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
</code></pre></div>
<p>since <a href="https://github.com/rust-lang/rust/issues/78461">#78461</a></p>



<a name="218076186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076186">(Nov 27 2020 at 10:25)</a>:</h4>
<p>considering that we don't have an <code>impl const AllocRef for Global</code> that is problematic, and we should probably close that hole</p>



<a name="218076259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076259">(Nov 27 2020 at 10:26)</a>:</h4>
<p>basically the const safety checks do not check defaulted generic parameters</p>



<a name="218076341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076341">(Nov 27 2020 at 10:26)</a>:</h4>
<p>because if the impl were <code>impl&lt;T, A: AllocRef&gt; Vec&lt;T, A&gt;</code> then we would get an error</p>



<a name="218076414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076414">(Nov 27 2020 at 10:27)</a>:</h4>
<p>but while writing this I realized that it's</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">Global</span><span class="o">&gt;</span><span class="w"></span>
</code></pre></div>
<p>which is fine, since we can't possibly call any methods on <code>Global</code> unless they are inherent and <code>const</code></p>



<a name="218076490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218076490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218076490">(Nov 27 2020 at 10:28)</a>:</h4>
<p>so... TLDR: Everything works, but we won't be able to use <code>Vec::new()</code> if we later want to <code>push</code></p>



<a name="218077027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218077027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218077027">(Nov 27 2020 at 10:33)</a>:</h4>
<p>reading that API I think that the type based approach really is the right way to go about all of this. We'll just never turn <code>impl AllocRef for Global</code> into a <code>const</code> impl, thus statically preventing any such allocations in const eval. For transient allocations we can then let the user choose a favorite crate that implements <code>AllocRef</code> for some stack/array based arena allocator.</p>



<a name="218077116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218077116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218077116">(Nov 27 2020 at 10:34)</a>:</h4>
<p>For non-transient allocations we'll provide <code>ConstGlobal</code> which never deallocates and requires the use of the cross <code>AllocRef</code> APIs</p>



<a name="218077209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218077209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218077209">(Nov 27 2020 at 10:35)</a>:</h4>
<p>it's not the magic bullet that gives us transient <code>serde_json</code> without threading a generic <code>AllocRef</code> parameter through everywhere, but it's completely sound and easy to understand imo</p>



<a name="218100412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218100412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218100412">(Nov 27 2020 at 14:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/218075911">said</a>:</p>
<blockquote>
<p>ok... So... I found a problem with our current plan. It would be a breaking change. When looking at <a href="https://github.com/rust-lang/rust/pull/78461">https://github.com/rust-lang/rust/pull/78461</a> by <span class="user-mention silent" data-user-id="216785">Tim Diekmann</span> I realized that <a href="https://github.com/rust-lang/wg-allocators/issues/1#issuecomment-488990394">it's a breaking change</a> to make <code>Vec::new</code> generic over the <code>AllocRef</code> being used. I'm not sure in what situations it's a problem.</p>
</blockquote>
<p>Currently this is a breaking change but I think this could be solved, if the compiler takes default generics into account when infering the type. But as you noted, this is not a problem here.</p>
<blockquote>
<p>Everything works, but we won't be able to use <code>Vec::new()</code> if we later want to <code>push</code></p>
</blockquote>
<p>Is this even true if <code>A == Global</code>? If so: Is it possible to work around this with specialization?</p>



<a name="218100694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218100694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218100694">(Nov 27 2020 at 15:00)</a>:</h4>
<p><code>Global</code> will not have an <code>impl const AllocRef for Global</code>, so you won't be able to invoke <code>push</code> which has an <code>A: AllocRef</code> bound</p>



<a name="218100895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218100895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218100895">(Nov 27 2020 at 15:02)</a>:</h4>
<p>I'm not sure how specialization could help here</p>



<a name="218100934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218100934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218100934">(Nov 27 2020 at 15:02)</a>:</h4>
<p>as the type of the <code>Vec</code> would be wrong (<code>Vec&lt;T, Global&gt;</code>)</p>



<a name="218101581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218101581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218101581">(Nov 27 2020 at 15:09)</a>:</h4>
<p>If this could compile, we would be fine:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="nc">AllocRef</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">push</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">Global</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">push</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218101895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218101895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218101895">(Nov 27 2020 at 15:12)</a>:</h4>
<p>well... the other way around with the <code>const</code>, but yes</p>



<a name="218101909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218101909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218101909">(Nov 27 2020 at 15:12)</a>:</h4>
<p>thanks, that explains the specialization case very well</p>



<a name="218102080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218102080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218102080">(Nov 27 2020 at 15:14)</a>:</h4>
<p>I don't know much about const-eval, just trying to help here :)</p>



<a name="218120343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218120343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218120343">(Nov 27 2020 at 18:51)</a>:</h4>
<p>(deleted)</p>



<a name="218120489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218120489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218120489">(Nov 27 2020 at 18:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/218076414">said</a>:</p>
<blockquote>
<p>but while writing this I realized that it's</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">Global</span><span class="o">&gt;</span><span class="w"></span>
</code></pre></div>
<p>which is fine, since we can't possibly call any methods on <code>Global</code> unless they are inherent and <code>const</code></p>
</blockquote>
<p>I am very confused, why would it be fine here but not for any other trait?<br>
EDIT: Oh, is it because the function is not actually generic? Ah makes sense.</p>



<a name="218120639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218120639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218120639">(Nov 27 2020 at 18:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/218077027">said</a>:</p>
<blockquote>
<p>reading that API I think that the type based approach really is the right way to go about all of this. We'll just never turn <code>impl AllocRef for Global</code> into a <code>const</code> impl, thus statically preventing any such allocations in const eval. For transient allocations we can then let the user choose a favorite crate that implements <code>AllocRef</code> for some stack/array based arena allocator.</p>
</blockquote>
<p>to be clear, you still agree we could have transient allocations with <code>Global</code> but we'd have to check post-monomorphization that none of these global ptrs make it to the final value?</p>



<a name="218121114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218121114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218121114">(Nov 27 2020 at 19:02)</a>:</h4>
<p>yes</p>



<a name="218121160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218121160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218121160">(Nov 27 2020 at 19:02)</a>:</h4>
<p>I still think we should have some opt-in or sth, but we can bikeshed that later and just have it behind a feature gate in the beginning</p>



<a name="218161004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218161004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218161004">(Nov 28 2020 at 11:53)</a>:</h4>
<p>I think my write up on const heap is now up to date to the discussion here: <a href="https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA">https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA</a></p>



<a name="218304219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218304219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218304219">(Nov 30 2020 at 15:47)</a>:</h4>
<p>The <code>push</code> issue also arose to me when I thought about this some more, and it is unfortunate indeed</p>



<a name="218304333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218304333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218304333">(Nov 30 2020 at 15:48)</a>:</h4>
<p>I think what I am missing is a clear sense for what people would like to sue heap-in-const <em>for</em>, to determine where the priorities are and how severe the various problems are that we uncovered.</p>



<a name="218320616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218320616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218320616">(Nov 30 2020 at 17:47)</a>:</h4>
<p><span class="user-mention" data-user-id="355545">@vn-ki</span> will be/is working on this, so we'll have <em>something</em> really unstable to play with</p>



<a name="218320817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218320817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218320817">(Nov 30 2020 at 17:49)</a>:</h4>
<p>I think if we go with the full "no <code>Global</code>" system to start out with, then we have at least the minimal subset that I currently believe is sound. Any extensions can go behind other feature gates and we can then discuss them separately</p>



<a name="218417635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218417635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218417635">(Dec 01 2020 at 12:55)</a>:</h4>
<blockquote>
<p>what people would like to sue heap-in-const <em>for</em></p>
</blockquote>
<p>A key one I’ve seen though Stack Overflow is something effectively:</p>
<div class="codehilite"><pre><span></span><code>const FOO: &amp;str = “a”.repeat(10);
</code></pre></div>
<p>Or similar; build up a value that could be expressed as a constant but is more understandable as the construction process.</p>



<a name="218417970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218417970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218417970">(Dec 01 2020 at 12:58)</a>:</h4>
<p>API wise, how bad do you think it is that you'll need a <code>leak</code> call or change the type to <code>String&lt;ConstGlobal&gt;</code>?</p>



<a name="218418003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418003">(Dec 01 2020 at 12:58)</a>:</h4>
<p>Should we put a focus on making <code>const FOO: &amp;str = &amp;some_heap_string;</code> work via autoderef?</p>



<a name="218418056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418056">(Dec 01 2020 at 12:59)</a>:</h4>
<p>or, alternatively, focus on transient allocations and how to safely "transport" them to the runtime world</p>



<a name="218418192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418192">(Dec 01 2020 at 13:00)</a>:</h4>
<p>yea... I just realized that making <code>repeat</code> return a <code>String&lt;ConstGlobal&gt;</code> may not be doable backwards compatibly</p>



<a name="218418221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418221">(Dec 01 2020 at 13:00)</a>:</h4>
<p>It may be, but I haven't read up enough on this</p>



<a name="218418478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418478">(Dec 01 2020 at 13:03)</a>:</h4>
<p>Still waking up completely morning thought, could something like <code>const{}</code> transfer <code>String&lt;GlobalAlloc&gt;</code> to <code>&lt;ConstAlloc&gt;</code></p>



<a name="218418512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418512">(Dec 01 2020 at 13:03)</a>:</h4>
<p>Or maybe a <code>.to_const()</code></p>



<a name="218418579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418579">(Dec 01 2020 at 13:04)</a>:</h4>
<p>(<code>leak</code> sounds wrong to me)</p>



<a name="218418598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418598">(Dec 01 2020 at 13:04)</a>:</h4>
<p>I don't think so, someone may have created a <code>struct MyString(*const ())</code> which actually points to a <code>Box&lt;Global&gt;</code> that was <code>Box::into_raw</code>ed.</p>



<a name="218418617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418617">(Dec 01 2020 at 13:04)</a>:</h4>
<p>so we wouldn't know any of this from the type system at all</p>



<a name="218418659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418659">(Dec 01 2020 at 13:05)</a>:</h4>
<p>I do have a setup that can work, but it will cause post-monorphization errors</p>



<a name="218418759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218418759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218418759">(Dec 01 2020 at 13:06)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> I'm talking about <code>leak</code> because of <a href="https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak">https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak</a></p>



<a name="218419075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218419075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218419075">(Dec 01 2020 at 13:08)</a>:</h4>
<p>The autoderef and copying transient data path feels like it would cover a lot of goals that people would want. It’s not quite as logically consistent s as what it sounds like y’all would want though.</p>



<a name="218419786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218419786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218419786">(Dec 01 2020 at 13:15)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> sure, but you don’t really need to <em>leak</em> anything in a bunch of these cases. The String would be allocated and deallocated at compile time and the string data “interned” in the generated code.</p>



<a name="218419842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218419842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218419842">(Dec 01 2020 at 13:16)</a>:</h4>
<p>well... that is leaking <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="218419860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218419860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218419860">(Dec 01 2020 at 13:16)</a>:</h4>
<p>(in my mind)</p>



<a name="218419898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218419898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218419898">(Dec 01 2020 at 13:16)</a>:</h4>
<p>I don't think we have a good picture yet of what is achievable, so we're mostly collecting data and building prototypes</p>



<a name="218419939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218419939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218419939">(Dec 01 2020 at 13:17)</a>:</h4>
<p>There is one system that is very verbose that will work (<code>ConstGlobal</code> everywhere), and we may want it in general as the first step and figure out better things on top of it</p>



<a name="218420062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420062">(Dec 01 2020 at 13:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/218419939">said</a>:</p>
<blockquote>
<p>There is one system that is very verbose that will work (<code>ConstGlobal</code> everywhere), and we may want it in general as the first step and figure out better things on top of it</p>
</blockquote>
<p>I feel like that is orthogonal to the <code>ConstHeapSafeRef</code> thing</p>



<a name="218420266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420266">(Dec 01 2020 at 13:20)</a>:</h4>
<p>Regarding <a href="https://github.com/rust-lang/rust/pull/79594#discussion_r533389909">this comment</a>, what does leak exactly mean?</p>



<a name="218420296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420296">(Dec 01 2020 at 13:20)</a>:</h4>
<p>If it's a memory leak, isnt heap leaking rn because we dont dealloc memory?</p>



<a name="218420348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420348">(Dec 01 2020 at 13:21)</a>:</h4>
<p>I think that verbose makes sense as a complete base case. It’s also nice if you can provide some small syntax sugar for the common cases (e.g. the auto deref thing)</p>



<a name="218420421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420421">(Dec 01 2020 at 13:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="355545">vn-ki</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval/near/218420266">said</a>:</p>
<blockquote>
<p>Regarding <a href="https://github.com/rust-lang/rust/pull/79594#discussion_r533389909">this comment</a>, what does leak exactly mean?</p>
</blockquote>
<p>leaking is used by miri to make sure the program deallocates all memory</p>



<a name="218420427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420427">(Dec 01 2020 at 13:21)</a>:</h4>
<p>I think it currently has no effect for CTFE</p>



<a name="218420488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420488">(Dec 01 2020 at 13:22)</a>:</h4>
<p>so I'd vote for doing the sensible thing and considering ConstHeap allocs not allwoed to leak</p>



<a name="218420532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420532">(Dec 01 2020 at 13:22)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> an alternative might be to make <code>ConstHeap</code> part of the <code>MachineMemory</code> variants in the CTFE machine instance, like what Miri does.</p>



<a name="218420708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218420708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218420708">(Dec 01 2020 at 13:24)</a>:</h4>
<p>Hmm... that's true, I wonder if that also applies to other variants like <code>Vtable</code>?</p>



<a name="218421596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218421596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218421596">(Dec 01 2020 at 13:32)</a>:</h4>
<blockquote>
<p>leaking is used by miri to make sure the program deallocates all memory </p>
</blockquote>
<p>I’m sure this makes sense somehow, but that’s a silly sentence 🤪</p>



<a name="218421934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218421934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218421934">(Dec 01 2020 at 13:35)</a>:</h4>
<p>I think the long version is: if a memory kind is marked as "leaking", then it is ignored by the leak checker in miri. The leak checker will go through all allocations after a program is done and inform you about all leaked allocations. So if heap were marked as leaking, then any left over heap allocations that weren't deallocated would be ignored. Since this is about <code>ConstHeap</code>, that's actually what we want, but irrelevant anyway as miri (the tool) will never create any of these. So we can mark it as non-leaking and thus catch more bugs</p>



<a name="218422824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218422824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218422824">(Dec 01 2020 at 13:43)</a>:</h4>
<p>I guess for us out of the loop, it might be better described as being marked as <em>deliberately</em> leaked</p>



<a name="218427119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218427119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218427119">(Dec 01 2020 at 14:18)</a>:</h4>
<p>I dont think we usually talk about "marking memory as leaking"... the method is called <code>may_leak</code></p>



<a name="218427291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218427291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218427291">(Dec 01 2020 at 14:19)</a>:</h4>
<p>This might require GATs, but could we do something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">ConvertAllocator</span><span class="o">&lt;</span><span class="n">Alloc</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Item</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">to_allocator</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span>::<span class="n">Item</span><span class="o">&lt;</span><span class="n">Alloc</span><span class="o">&gt;</span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">constify</span><span class="p">(</span><span class="n">v</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">ConvertAllocator</span><span class="o">&lt;</span><span class="n">ConstAlloc</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="cm">/* or maybe HKTs... */</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">todo</span><span class="o">!</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Used as</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">Foo</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">ConstAlloc</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">constify</span><span class="p">({</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Merry String actions</span>
<span class="p">})</span><span class="w"></span>
</code></pre></div>



<a name="218428528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218428528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218428528">(Dec 01 2020 at 14:28)</a>:</h4>
<p>sure, we can add more sugar, but the root problem is that people who do not use it will break the system, because there's no way to (statically) prevent them from putting a <code>String&lt;Global&gt;</code> into a constant</p>



<a name="218429861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218429861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218429861">(Dec 01 2020 at 14:39)</a>:</h4>
<p>With dynamic checks we can prevent that, right?</p>



<a name="218429933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218429933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218429933">(Dec 01 2020 at 14:39)</a>:</h4>
<p>I edited and added <code>(statically)</code></p>



<a name="218430147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218430147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218430147">(Dec 01 2020 at 14:41)</a>:</h4>
<p>I think I forgot why we absolutely prefer static over dynamic checks. can you clarify it to me _once_ more</p>



<a name="218430727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218430727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218430727">(Dec 01 2020 at 14:46)</a>:</h4>
<p>Re-reading this helped: <a href="https://github.com/rust-lang/const-eval/blob/master/const_checks.md">https://github.com/rust-lang/const-eval/blob/master/const_checks.md</a></p>



<a name="218431026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218431026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218431026">(Dec 01 2020 at 14:48)</a>:</h4>
<p>dynamic checks are similar to duck typing. Everything works until it doesn't, and it may fail in arbitrary crates, not necessarily in the one that did the mistake, often without a real way to figure out how the error relates to the mistake</p>



<a name="218440276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218440276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218440276">(Dec 01 2020 at 15:53)</a>:</h4>
<p>And Rust programmers in general prefer things to be caught at compile time. Part of liking Rust.</p>



<a name="218440653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218440653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218440653">(Dec 01 2020 at 15:55)</a>:</h4>
<p>jup, even if said runtime is a const eval VM that is actually only used for compile-time stuff in rustc</p>



<a name="218440863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218440863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218440863">(Dec 01 2020 at 15:56)</a>:</h4>
<p>Should have just created a new LLVM backend for the Miri VM. Should make things easier <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="218443152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218443152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218443152">(Dec 01 2020 at 16:10)</a>:</h4>
<p>Rewrite it in Wasm. Or eBPF.</p>



<a name="218443227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218443227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218443227">(Dec 01 2020 at 16:11)</a>:</h4>
<p>you really know the best kinds of gasoline to throw onto a fire</p>



<a name="218444605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218444605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218444605">(Dec 01 2020 at 16:18)</a>:</h4>
<p>Obviously I don't want any of that because it will delay my const-eval gratification</p>



<a name="218447286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Questions%20regarding%20Heap%20allocation%20in%20const%20eval/near/218447286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Questions.20regarding.20Heap.20allocation.20in.20const.20eval.html#218447286">(Dec 01 2020 at 16:36)</a>:</h4>
<p>A Cranelift backend would compile faster <span aria-label="stuck out tongue wink" class="emoji emoji-1f61c" role="img" title="stuck out tongue wink">:stuck_out_tongue_wink:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>